package net.mikoo.seals.biz.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;

import net.mikoo.seals.biz.common.BaseServices;
import net.mikoo.seals.biz.manager.ApplyManager;
import net.mikoo.seals.biz.mapper.ApplyMapper;
import net.mikoo.seals.biz.mapper.ApplyVerifyMapper;
import net.mikoo.seals.biz.service.ApplyServices;
import net.mikoo.seals.common.enums.ApplyStatusEnum;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.model.Apply;
import net.mikoo.seals.model.ApplyQuery;
import net.mikoo.seals.model.Upfile;


@Service
public class ApplyServicesImpl extends BaseServices implements ApplyServices {
	
	private Logger logger = LoggerFactory.getLogger(ApplyServicesImpl.class);

	@Autowired
	private ApplyManager applyManager;
	
	@Autowired
	private ApplyMapper applyMapper;
	
	@Autowired
	private	ApplyVerifyMapper applyVerifyMapper;
	
	
	@Override
	public void apply(Apply apply) {
		applyManager.checkParams(apply);
		
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				
				try {
					applyManager.exeApplyProcessor(apply);
				}catch (ParametersException e) {
					logger.error("",e);
					status.setRollbackOnly();
					throw e;
				}catch (Exception e) {
					logger.error("",e);
					status.setRollbackOnly();
					throw e;
				}
			}
		});
	}
	
	
	@Override
	public PageModel<Apply> getPageByQuery(ApplyQuery query) {
		
		List<Apply> datas = applyMapper.selectByQueryToPage(query);
		Integer total = applyMapper.countByQueryToPage(query);
		PageModel<Apply> pm = new PageModel<>(total,datas);
		
		return pm;
	}

	@Override
	public Apply getById(Long applyId) {
		if(applyId == null) {
			throw new ParametersException("applyId");
		}
		return applyMapper.selectInfoById(applyId);
	}


	@Override
	public List<Upfile> getApplyfiles(Long applyId) {
		if(applyId == null) {
			throw new ParametersException("applyId is null");
		}
		
		List<Upfile> upfiles = applyMapper.selectApplyfiles(applyId);
		
		return upfiles;
	}


	@Override
	public Apply getInfoByQuery(ApplyQuery query) {
		
		if(query==null) {
			throw new ParametersException("query is null");
		}
		
		if(query.getApplyId() == null) {
			throw new ParametersException("ApplyId is null");
		}
		
		
		Apply apply = applyMapper.selectInfoByQuery(query);
		
		return apply;
	}


	@Override
	public void retract(ApplyQuery query) {
		if(query==null) {
			throw new ParametersException("query is null");
		}
		
		if(query.getApplyId() == null) {
			throw new ParametersException("ApplyId is null");
		}
		
		if(query.getCompanyId() ==null) {
			throw new ParametersException("CompanyId is null");
		}
		
		if(query.getPartyId() ==null) {
			throw new ParametersException("PartyId is null");
		}
		
		Apply apply = applyMapper.selectInfoByQuery(query);
		
		if(apply == null) {
			throw new ParametersException("申请不存在");
		}
		
		if(!apply.getApplyStatus().equals(ApplyStatusEnum.PENDING.code())) {
			throw new ParametersException("当前状态不处于待审核状态");
		}
		
		this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				
				apply.setApplyStatus(ApplyStatusEnum.RETRACT.code());
				applyMapper.updateChangeStatus(apply);
				applyVerifyMapper.updateToRetractByApplyId(apply.getApplyId());
				
			}
		});
		
	}


	@Override
	public List<Apply> getListByQuery(ApplyQuery query) {
		List<Apply> applys = applyMapper.selectListByQuery(query);
		return applys;
	}

	
}
